package com.na.onlineservey

import com.docu.common.Message
import org.springframework.beans.factory.annotation.Autowired
import grails.converters.JSON
import com.na.onlineservey.project.CreateProjectAction
import com.na.onlineservey.project.UpdateProjectAction
import com.na.onlineservey.project.ListProjectAction
import com.na.onlineservey.project.DeleteProjectAction
import com.na.onlineservey.project.ReadProjectAction
import com.na.onlineservey.project.SearchProjectAction

class ProjectController {

    @Autowired
    private CreateProjectAction createProjectAction
    @Autowired
    private UpdateProjectAction updateProjectAction
    @Autowired
    private ListProjectAction listProjectAction
    @Autowired
    private DeleteProjectAction deleteProjectAction
    @Autowired
    private ReadProjectAction readProjectAction
    @Autowired
    private SearchProjectAction searchProjectAction

    @Autowired
    ProjectService projectService
                    static allowedMethods = [create: "POST", update: "POST", delete: "POST"]

    def list = {
       List list = listProjectAction.execute(params, null)
       render listProjectAction.postCondition(null, list) as JSON
    }

    def show = {
      Project projectInstance = new Project()
      render(template: "show", model:[projectInstance:projectInstance])
    }

    def create = {
      Project projectInstance = new Project(params)
      Project projectInstanceInstance = createProjectAction.preCondition(null, projectInstance)
      Message message = null
      if (projectInstanceInstance == null) {
        message = createProjectAction.getValidationErrorMessageForUI(projectInstance)
      } else {
        projectInstanceInstance = createProjectAction.execute(null, projectInstanceInstance)
        if (projectInstanceInstance) {
          message = createProjectAction.getSuccessMessageForUI(projectInstanceInstance, createProjectAction.SUCCESS_SAVE)
        } else {
          message = createProjectAction.getErrorMessageForUI(projectInstance, createProjectAction.FAIL_SAVE)
        }
      }
      render message as JSON
    }

    def edit = {
      render readProjectAction.execute(params, null) as JSON
    }

    def update = {
      Project projectInstance = new Project(params)
      Object object = updateProjectAction.preCondition(params,null)
      Message message = null
      if (object == false) {
          message = updateProjectAction.getValidationErrorMessageForUI(projectInstance)
      } else {
          int noOfRows = (int) updateProjectAction.execute(null,object)
          if (noOfRows > 0) {
            message = updateProjectAction.getSuccessMessageForUI(projectInstance, updateProjectAction.SUCCESS_UPDATE)
          } else {
            message = updateProjectAction.getErrorMessageForUI(projectInstance, updateProjectAction.FAIL_UPDATE)
          }
      }
      render message as JSON
    }

    def delete = {
        
       Project projectInstance = deleteProjectAction.preCondition(params,null)
        Message message = null
        if (projectInstance) {
          int rowCount = (int) deleteProjectAction.execute(null, projectInstance)
          if (rowCount > 0) {
            message = deleteProjectAction.getSuccessMessageForUI(projectInstance, deleteProjectAction.SUCCESS_DELETE)
          } else {
            message = deleteProjectAction.getErrorMessageForUI(projectInstance, deleteProjectAction.FAIL_DELETE)
          }
        }
        else {
          message = deleteProjectAction.getErrorMessageForUI(projectInstance, deleteProjectAction.ALREADY_DELETED)
        }
        render message as JSON
    }

    def search = {
      Project projectInstance = searchProjectAction.execute(params.fieldName, params.fieldValue)
      if(projectInstance){
          render projectInstance as JSON
      }
      else{
          render ''
      }

    }
}
